package net.onecloud.onecloud.account;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.account.record.RecordActivity;
import net.onecloud.onecloud.account.resume.ResumeActivity;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BasePage;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.utils.PreferencesUtils;


/**
 * Created by zaizai on 16/7/26.
 */
public class AccountPage extends BasePage implements View.OnClickListener {

    private TextView mUsername;
    private Button mBTLogin;
    private LinearLayout mResume;
    private Button mBTLogout;
    private LinearLayout mRecord;

    public AccountPage(Activity activity) {
        super(activity);
        initView();
        initListener();


    }

    private void initListener() {
        mBTLogin.setOnClickListener(this);
        mResume.setOnClickListener(this);
        mBTLogout.setOnClickListener(this);
        mRecord.setOnClickListener(this);
    }

    public void initView() {
        View view = View.inflate(mActivity, R.layout.page_account, null);
        mUsername = (TextView) view.findViewById(R.id.m_username);
        mBTLogin = (Button) view.findViewById(R.id.m_bt_login);
        mBTLogout = (Button) view.findViewById(R.id.m_bt_logout);
        mResume = (LinearLayout) view.findViewById(R.id.m_account_resume);
        mRecord = (LinearLayout) view.findViewById(R.id.m_record);

        this.mFrameLayoutContent.addView(view);
    }

    @Override
    public void initData() {
        ResultUser.DataEntity user = App.getUser();
        if (user != null) {
            mUsername.setVisibility(View.VISIBLE);
            mBTLogout.setVisibility(View.VISIBLE);
            mBTLogin.setVisibility(View.GONE);
            mUsername.setText(user.getUsername());
        } else {
            mUsername.setVisibility(View.GONE);
            mBTLogout.setVisibility(View.GONE);
            mBTLogin.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.m_bt_login: {
                Intent intent = new Intent(mActivity, LoginActivity.class);
                mActivity.startActivityForResult(intent, 3);
                break;
            }
            case R.id.m_account_resume: {
                if (App.getUser() == null) {
                    showHit("您还没有登录哦!");
                    break;
                }
                Intent intent = new Intent(mActivity, ResumeActivity.class);
                mActivity.startActivity(intent);
                break;
            }
            case R.id.m_bt_logout: {
                cleartUser();
                break;
            }
            case R.id.m_record: {
                if (App.getUser() == null) {
                    showHit("您还没有登录哦!");
                    break;
                }
                Intent intent = new Intent(mActivity, RecordActivity.class);
                mActivity.startActivity(intent);
                break;
            }
        }

    }

    private void cleartUser() {
        App.setUser(null);
        mUsername.setVisibility(View.GONE);
        mBTLogout.setVisibility(View.GONE);
        mBTLogin.setVisibility(View.VISIBLE);
    }

    protected void showHit(String message) {
        new AlertDialog.Builder(mActivity)
                .setTitle("提示")
                .setMessage(message)
                .setPositiveButton("确定", null)
                .show();
    }
}
